package com.pengtu.gsj.service.impl.check.anqsc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.system.AttachmentDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.anqsc.EmergSystemSecService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class EmergSystemSecServiceImpl implements EmergSystemSecService{

	
	@Autowired
	public SystemManageService systemManageService;
	@Autowired
	public CheckTableService checkTableService;
	@Autowired
	public AttachmentDao attachmentDao; 
	
	@Override
	public Map<String, Object> getEmergSystemSecs(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		List<Attachment> list = new ArrayList<Attachment>();
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object> values = new ArrayList<Object>();
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double tableFull = Double.parseDouble((String)jsonObject.get("yearFull")); //预设满分
		double tempScore = MathsUtils.divDouble(tableFull, 5, 1); //每个小项的得分
		double total = 0 ;//计算得分
		double yjyxjzScore = 0; //应急运行机制得分
		//根据时间、标签查询是否有附件
		list = systemManageService.getCheckAttachments(startTime, endTime, tag);
		// 检查对应的应急预案、应急演示是否有附件，有的话得5分之一总分，没有就得0分
		double yatxScore = systemManageService.getCheckAttachmentByTag(startTime, endTime, "yjtx_yjya").size() > 0 ? tableFull / 5 : 0;
		double yjylScore = systemManageService.getCheckAttachmentByTag(startTime, endTime, "yjtx_yjys").size() > 0 ? tableFull / 5 : 0;
		//应急运行机制得分根据应急预案、应急演示是否得分判断
		if (Math.abs(yatxScore + yjylScore - 2*tempScore) == 0) {
			 yjyxjzScore = tempScore;
		} else if (Math.abs(yatxScore + yjylScore - tempScore) == 0) {
			yjyxjzScore = MathsUtils.divDouble(tableFull, 10, 1);
		}
		yatxScore = MathsUtils.formatDouble(yatxScore, 1);
		yjyxjzScore = MathsUtils.formatDouble(yjyxjzScore, 1);
		yjylScore = MathsUtils.formatDouble(yjylScore, 1);
		double bjszScore = tempScore;
		double jscbScore = tempScore;
		//总得分和完善率
		total = MathsUtils.formatDouble((yatxScore + yjylScore + bjszScore + jscbScore + yjyxjzScore), 1);
		double percentage = MathsUtils.divDouble(total * 100, tableFull, 2);
		values.add("true");
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		//预案体系得分
		map.put("yatxScore", yatxScore);
		//应急运行机制得分：
		map.put("yjyxjzScore", yjyxjzScore);
		//应急演练得分
		map.put("yjylScore", yjylScore);
		//布局设置得分
		map.put("bjszScore", bjszScore);
		//建设储备得分
		map.put("jscbScore", jscbScore);
		map.put("att", list);
		map.put("percent", percentage);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}
}
